package com.kzw.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.kzw.entity.Record;

import my.db.Page;
import my.db.QueryHelper;

public class RecordDao {
	// 增加
			public void save(Record a) {
				String sql = "insert into record values(null,?,?,?,?,?)";
				QueryHelper.update(sql,a.getUid(),a.getUname(),a.getGid(),a.getGname(),a.getCtime());
			}

			public void delete(Long rid) {
				String sql = "delete from record where rid=?";
				QueryHelper.update(sql, rid);
			}
			
			/**
			 * 分页查询
			 * */
			public Page<Record> findPage(Page<Record> page, String uname) {
				List<Object> params = new ArrayList<>();
				String sql = "select * from record";
				if(StringUtils.isNotBlank(uname)) {
					sql += " where uname like ?";
					params.add("%"+uname+"%");
				}
				if(StringUtils.isNotBlank(page.getField())) {
					sql += String.format(" order by %s %s", page.getField(), page.getType());
				}
				
				List<Record> data = QueryHelper.queryPage(Record.class, sql, page.getPage(), page.getLimit(), params.toArray());
				page.setData(data);
				
				String sql2 = sql.replaceFirst("select .*? from", "select count(*) from");
				Long cnt = QueryHelper.stat(sql2, params.toArray());
				page.setCount(cnt);			
				
				return page;
			}
			

}
